package Syntaxe;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;

import Simplification.PreSimplification;
import Simplification.Simplification;

public class Invariant {
	public LinkedList<Expression> listExpr;
	public int numExpr;
	//public LinkedHashSet<String> SetVar;
	//public int numVar;
	public Invariant(){
		listExpr=new LinkedList<Expression> ();
		numExpr=0;
	}

	public Invariant(LinkedList<Expression> list) {
		listExpr = list;
		numExpr = 0;

		while (!list.isEmpty()) {
			list.poll();
			numExpr = numExpr + 1;
		}
		// TODO
		// 第一個expr是否有全部var?
	//	SetVar = new LinkedHashSet<String>(listExpr.peek().subList.keySet());
	//	numVar = SetVar.size();
	}

	public int numVar() {
		int i = 0;
		if (numExpr != 0) {
			if (listExpr.peek() != null) {
				i = listExpr.peek().size();
			}
		}
		return i;

	}

	public LinkedHashSet<String> SetVar() {
		int N = this.numVar();
		// Set<String> SetVar = listExpr.peek().subList.keySet();

		LinkedHashSet<String> SetVar = new LinkedHashSet<String>(
				listExpr.peek().subList.keySet());
		// for (String s : listExpr.peek().subList.keySet()) {
		// listVar.add(s);
		// }

		return SetVar;
	}

	public void ajouterExpression(Expression expr) {
		listExpr.add(expr);
		numExpr = numExpr + 1;
	}
	public Expression poll(){
		Expression expr=listExpr.poll();
		numExpr = numExpr - 1;
		return expr;
	}

	public void supremerAllExpression() {
		listExpr.clear();
		numExpr = 0;

	}

	public void afficherInvariant() {
		LinkedList<Expression> toshow = listExpr;// ????? will listExpr change?
		while (!toshow.isEmpty()) {
			toshow.poll().affiche();
		}
	}

	public Matrice transAMatrice() {

		int M = this.numExpr;
		int N = this.numVar(); // numbre de variable
		Matrice m = new Matrice(M, N + 1);

		LinkedList<Expression> list = this.listExpr;
		int i = 0;
		int j = 0;
		LinkedList<String> Var = new LinkedList<String>(); // ??????
		while (!list.isEmpty()) {
			Expression expr = list.poll();

			for (String s : this.SetVar()) {
				m.matrice[i][j] = expr.subList.get(s);
				j++;
			}
			m.matrice[i][j] = expr.constantOfExpression;
			i++;
		}
		return m;
	}


	

	// TODO
	

}
